import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.pub.web.WebFunction;
import com.pub.database.QueryBean;

public class openAttach extends HttpServlet {
    //private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

	static final long serialVersionUID = 1L;
	
    //Initialize global variables
    public void init() throws ServletException {
    }

    //Process the HTTP Get request
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        //response.setContentType(CONTENT_TYPE);
       // response.addHeader("Content-Disposition", "inline");
        QueryBean lqbnDAO=new QueryBean(request.getParameter("db_alias"),false,"BIG5","BIG5");

        (new WebFunction()).setPortal_to_session(request);
        if(request.getParameter("file_storage").toUpperCase().equals("DB")){
            try{
                (new com.pub.tools.AttachBean()).downloadAttach(response,
                        lqbnDAO, request.getParameter("table"),
                        request.getParameter("flow_code"),
                        request.getParameter("file_id"));

            }catch(Exception e){
                e.printStackTrace();
                if(lqbnDAO!=null){
                   try{lqbnDAO.close();}catch(Exception se){System.out.println("can't close querybean in pubpage.openAttach");}
                   lqbnDAO=null;
                    }
            }

        }

        try{
            String lstrFileName = lqbnDAO.readSingleValueData("select file_name from " +
                    request.getParameter("table") + " where flow_code='" +
                    request.getParameter("flow_code") + "' and file_id='" +
                    request.getParameter("file_id") + "'");

            lqbnDAO.executeSQL(
                    "insert into attach_log(flow_code,file_id,empno,action,file_name) values('" +
                    request.getParameter("flow_code") + "','" +
                    request.getParameter("file_id") + "','" +
                    request.getSession().getAttribute("u_id").toString() + "','open','" +
                    lstrFileName + "')");

        }
        catch(Exception e){
            e.printStackTrace();
            System.out.println("insert into attach_log error in pubpage.openAttach");
        }
        finally{
            if(lqbnDAO!=null){
                try{lqbnDAO.close();}catch(Exception se){System.out.println("can't close querybean in pubpage.openAttach");}
                lqbnDAO=null;
            }
        }
    }

    //Clean up resources
    public void destroy() {
    }
}
